
Plex.Settings.DND = {
'showTargets' : true,
}

Plex.Com.DND = function()
{
	var self=this;

	this.currentObject = false;
	this.currentClone = false;
	this.mouseOffest = false;

	this.handler = {
	'noTarget':function(){},
	'onDragStart':function(){},
	'onDragMove':function(){},
	'onDragFinish':function(){},
	}

	this.targets = [];

	this.getOffset = function(node)
	{
		var offset = {
			x : -(node.scrollLeft),
			y : -(node.scrollTop),
			w : -(node.offsetWidth),
			h : -(node.offsetHeight)
		}

		while(node)
		{
			offset.x += isNaN(node.offsetLeft) ? 0 : node.offsetLeft;
			offset.y += isNaN(node.offsetTop) ? 0 : node.offsetTop;
			node=node.offsetParent
		}

		return offset;
	}

	this.getMouseOffset = function (target, event)
	{
		var offset = self.getOffset(target);

		offset.x = event.pageX - offset.x;
		offset.y = event.pageY - offset.y;

		return offset;
	}

	this.getZIndex=function(obj)
	{
		if(!obj)
		return 0;

		while(true)
		{
			if(obj.parentNode==document.body)
			break;
			obj=obj.parentNode;
		}
		return parseInt(obj.style.zIndex);
	}

	this.dragMove = function(event)
	{
		if(self.currentClone)
		{
			self.currentClone.set({
				'styles':{
					'cursor': 'Move',
					'visibility': 'visible',
					'position': 'absolute',
					'top': event.pageY + window.scrollY - self.mouseOffset.y,
					'left': event.pageX + window.scrollX - self.mouseOffset.x
				}
			});
			self.handler.onDragMove(self.currentClone);
		}
	}

	this.dragUp = function(event)
	{
		var targetFound = false, targetAction = false;

		self.targets.each(function(target,x){

			var offset = self.getOffset(target);
			var left = offset.x, right = offset.x + offset.w, top = off.y, bottom = offset.y + offset.h;

			if((event.pageX>left)&&(event.pageX<right)&&(event.pageY>top)&&(event.pageY<bottom))
			{
				if( self.getZIndex(target) > self.getZIndex(targetFound) )
				{
					Plex.Debug("New Target"+target);
					targetFound=target;
					targetAction=targetActions[x];
				}
			}
		});

		if(targetFound)
		targetAction(self.currentClone,self.currentObject,event);
		else
		self.handler.noTarget(self.currentClone,self.currentObject,event);

		self.hideTargets();
		self.currentClone.remove();
		document.onmousemove = Summit.doNothing;
		document.onmouseup = Summit.doNothing;
	}

	this.showTargets = function(object)
	{
		if(Plex.Settings.DND.showTargets)
		{
			self.targets.each(function(target,x){
				targets[x].style.backgroundImage='url(images/target.png)';
				targets[x].style.minHeight=object.offsetHeight;
			})
		}
	}

	this.hideTargets = function()
	{
		if(Plex.Settings.DND.showTargets)
		{
			self.targets.each(function(target,x){
				targets[x].style.minHeight='';
				targets[x].style.backgroundImage='';
			})
		}
	}

	this.addTarget=function(object,action)
	{
		self.targetActions.push(action);
		self.targets.push(object);
	}

	this.removeTarget=function(ID)
	{
		delete self.targets[ID];
		delete self.targetActions[ID];
	}

	this.addObject=function(object)
	{
		object.DND=self;

		object.onmousedown = function(event)
		{
			if(event.shiftKey)
			{
				if(!object.allowPropagation)
				event.stopPropagation();

				self.showTargets(object);
				self.handler.onDragStart(object);
				self.currentObject = object;
				self.currentClone = object.clone(true);
				self.currentClone.set({
					'styles':{
						'opacity':.5,
						'zIndex':20000,
						'visibility':'hidden'
					}
				})
				

				self.mouseOffset = self.getMouseOffset(object, event);
				self.currentClone.injectInside(document.body);
				document.onmousemove = self.dragMove;
				
				document.onmouseup = self.dragUp;

				var target = event.target.tagName;

				if(target!='INPUT'&&target!='TEXTAREA'&&target!='SELECT'&&target!='OPTION')
				{
					document.body.blur();
					return false;
				}
				return true;
			}
		}
	}
}